package com.example.springboot.controller;

import com.example.springboot.common.Constants;
import com.example.springboot.common.Page;
import com.example.springboot.entity.IconDict;
import com.example.springboot.entity.Role;
import com.example.springboot.mapper.IconDictMapper;
import org.springframework.web.bind.annotation.*;
import com.example.springboot.common.Result;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.example.springboot.service.IMenuService;
import com.example.springboot.entity.Menu;

import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/menu")
    public class MenuController {
    
    @Resource
    private IMenuService menuService;

    @Resource
    private IconDictMapper iconDictMapper;

    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody Menu menu) {
        return Result.success(menuService.saveOrUpdate(menu));
    }

    //根据id删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(menuService.removeById(id));
    }

    //根据id批量删除
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(menuService.removeByIds(ids));
    }

    //查找所有
    @GetMapping
    public Result findAll(@RequestParam(defaultValue = "") String name) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",name);

        //查询所有数据
        List<Menu> list = menuService.list(queryWrapper);
        //找出pid为null的一级菜单
        List<Menu> parentNode = list.stream().filter(menu -> menu.getPid() == null).collect(Collectors.toList());
        //找出一级菜单的子菜单
        for(Menu menu : parentNode)
            //筛选所有数据中pid=父级id的数据就是二级菜单
            menu.setChildren(list.stream().filter(m -> menu.getId().equals(m.getPid())).collect(Collectors.toList()));
        return Result.success(parentNode);
    }

    //根据id查找
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(menuService.getById(id));
    }

    @GetMapping("/findPageByMenuname")
    public Result findPageByUsername(@RequestParam(defaultValue = "1") int PageNum,
                                     @RequestParam(defaultValue = "5") int PageSize,
                                     @RequestParam(defaultValue = "") String name) {
        Page page = new Page();
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",name);
        List<Menu> list = menuService.list(queryWrapper);
        if(list.isEmpty()) {
            return Result.error(Constants.CODE_600,null);
        }

        return Result.success(page.startPage(list,PageNum,PageSize));
    }

    @GetMapping("/getIcons")
    public Result getIcons() {
        QueryWrapper<IconDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type","icon");
        return Result.success(iconDictMapper.selectList(queryWrapper));
    }
}

